package com.zzy.jdk8.date;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

import org.junit.Test;

public class Java8DateUtils {

	@Test
	public void testLocalDate() {

		System.out.println("--------------------增-----------------------");
		LocalDate today = LocalDate.now();
		System.out.println("今天的日期是 : " + today);

		int year = today.getYear();
		int month = today.getMonthValue();
		int day = today.getDayOfMonth();
		System.out.printf("%d年 ------%d月------%d日  ", year, month, day);

		LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
		System.out.println("创建日期:" + dateOfBirth);

		
		System.out.println("--------------------加减日期-----------------------");
		LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
		System.out.println("一周后:" + nextWeek);
		LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
		System.out.println("前一年 : " + previousYear);
		LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
		System.out.println("后一年 : " + nextYear);
		
		
		System.out.println("--------------------比较日期-----------------------");
		
		LocalDate tomorrow = LocalDate.of(2017, 4, 24);
		
		System.out.println("比较日期是否相等"+tomorrow.equals(today));
		
		if (tomorrow.isAfter(today)) {
			System.out.println("Tomorrow comes after today");
		}
		LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);
		if (yesterday.isBefore(today)) {
			System.out.println("Yesterday is day before today");
		}

		
		System.out.println("--------------------区间日期-----------------------");
		LocalDate java8Release = LocalDate.of(2014, 4, 14);
		Period period = Period.between(today, java8Release);
		System.out.printf("2014-4-14 距今天相隔  %s 年  %s 月  %s 天: ",period.getYears()+"",period.getMonths()+"",period.getDays()+"");
		
		
		System.out.println("--------------------其他日期-----------------------");
		YearMonth currentYearMonth = YearMonth.now();
		System.out.printf("现在是%s,本月%s天", currentYearMonth, currentYearMonth.lengthOfMonth());
		

		if (today.isLeapYear()) {
			System.out.println("闰年");
		} else {
			System.out.println("不是闰年");
		}

	

	}

	@Test
	public void testLocalTime() {
		LocalTime time = LocalTime.now();
		System.out.println("local time now : " + time);

		LocalTime newTime = time.plusHours(2); // adding two hours
		System.out.println("两个小时后 : " + newTime);
	}
	
	
	

	@Test
	public void testLocalDateTime() {
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
		LocalDateTime dateTime = LocalDateTime.now();
		
		System.out.println(dateTime.format(format));
		
		LocalDateTime time2=dateTime.plusHours(2);
		
		System.out.println("两个小时后 : " + time2.format(format));
	}

	@Test
	public void testInstant() {
		Instant timestamp = Instant.now();
		System.out.println("What is value of this instant " + timestamp);
	}

	@Test
	public void testDateTimeFormatter() {
		LocalDateTime arrivalDate = LocalDateTime.now();
		try {
			DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
			String landing = arrivalDate.format(format);
			System.out.printf("格式化字符串 : %s", landing);
		} catch (DateTimeException ex) {
			ex.printStackTrace();
		}
	}
	
	
	@Test
	public void testConver() {
		System.out.println("java.time.LocalDateTime --> java.util.Date");
		LocalDateTime localDateTime = LocalDateTime.now();
	    ZoneId zone = ZoneId.systemDefault();
	    Instant instant = localDateTime.atZone(zone).toInstant();
	    java.util.Date date = Date.from(instant);
	    
	    
	    System.out.println("java.util.Date --> java.time.LocalDateTime");
	    java.util.Date date1 = new java.util.Date();
	    Instant instant1 = date1.toInstant();
	    ZoneId zone1 = ZoneId.systemDefault();
	    LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, zone1);
	    
	}
	
	
	

}
